package com.dead.config;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DeadConfig {

    public static final String DEAD_QUEUE="dead_queue";
    public static final String DEAD_EXCHANGE="dead_exchange";
    public static final String DEAD_ROUTE_KEY="dead_route_key";


    public static final String REJECT_QUEUE="reject_queue";
    public static final String REJECT_ROUTE_KEY="reject_route_key";
    public static final String REJECT_EXCHANGE_NAME="reject_exchange";


    public static final String TTL_QUEUE="ttl_queue";
    public static final String TTL_EXCHANGE_NAME="ttl_exchange";
    public static final String TTL_ROUTE_KEY="ttl_route_key";

    public static final String OVER_QUEUE="over_queue";
    public static final String OVER_EXCHANGE_NAME="over_exchange";
    public static final String OVER_ROUTE_KEY="over_route_key";

    @Bean
    public Queue deadQueue(){
        return QueueBuilder.nonDurable(DEAD_QUEUE).autoDelete().build();
    }
    @Bean
    public DirectExchange deadExchange(){
        return ExchangeBuilder.directExchange(DEAD_EXCHANGE).autoDelete().build();
    }
    @Bean
    public Binding deadBinding(){
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(DEAD_ROUTE_KEY);
    }


    /**
     * 消息被拒绝
     * @return
     */
    @Bean
    public Queue rejectQueue(){
        Map<String, Object> arguments =new HashMap<>();
        // 重点：：设置这两个参数
        //设置对列的死信交换机
        arguments.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        //设置死信路由key，要和死信交换机和死信队列绑定key一模一样，因为死信交换机是直连交换机
        arguments.put("x-dead-letter-routing-key",DEAD_ROUTE_KEY);
        return QueueBuilder.nonDurable(REJECT_QUEUE).autoDelete().withArguments(arguments).build();
    }
    @Bean
    public DirectExchange rejectExchange(){
        return ExchangeBuilder.directExchange(REJECT_EXCHANGE_NAME).autoDelete().build();
    }
    @Bean
    public Binding rejectBinding(){
        return BindingBuilder.bind(rejectQueue()).to(rejectExchange()).with(REJECT_ROUTE_KEY);
    }


    /**
     * 消息过期
     * @return
     */
    @Bean
    public Queue ttlQueue(){
        Map<String, Object> arguments =new HashMap<>();
        // 重点：：设置这两个参数
        //设置对列的死信交换机
        //队列过期时间
        arguments.put("x-message-ttl",5000);
        arguments.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        //设置死信路由key，要和死信交换机和死信队列绑定key一模一样，因为死信交换机是直连交换机
        arguments.put("x-dead-letter-routing-key",DEAD_ROUTE_KEY);
        return QueueBuilder.nonDurable(TTL_QUEUE).autoDelete().withArguments(arguments).build();
    }
    @Bean
    public DirectExchange ttlExchange(){
        return ExchangeBuilder.directExchange(TTL_EXCHANGE_NAME).autoDelete().build();
    }
    @Bean
    public Binding ttlBinding(){
        return BindingBuilder.bind(rejectQueue()).to(rejectExchange()).with(TTL_ROUTE_KEY);
    }


    /**
     * 消息超出队列大小
     * @return
     */
    @Bean
    public Queue overQueue(){
        Map<String, Object> arguments =new HashMap<>();
        // 重点：：设置这两个参数
        //设置对列的死信交换机
        //设置对列的最大长度
        arguments.put("x-max-length",0);
        arguments.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        //设置死信路由key，要和死信交换机和死信队列绑定key一模一样，因为死信交换机是直连交换机
        arguments.put("x-dead-letter-routing-key",DEAD_ROUTE_KEY);
        return QueueBuilder.nonDurable(OVER_QUEUE).autoDelete().withArguments(arguments).build();
    }
    @Bean
    public DirectExchange overExchange(){
        return ExchangeBuilder.directExchange(OVER_EXCHANGE_NAME).autoDelete().build();
    }
    @Bean
    public Binding overBinding(){
        return BindingBuilder.bind(overQueue()).to(overExchange()).with(OVER_ROUTE_KEY);
    }



}
